const mongoose = require('mongoose')
mongoose.connect('mongodb://127.0.0.1:27017/node-vue-blog', { useNewUrlParser: true, useUnifiedTopology: true })

const Article = mongoose.model('Article', new mongoose.Schema({
    title: { type: String, default: '' },
    tags: [{ type: mongoose.SchemaTypes.ObjectId, ref: 'Tag', default: [] }],
    body: { type: String, default: '' },
    description: { type: String, default: '' },
    titlePicture: { type: String, default: '' }
}, { timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' } }))

const Tag = mongoose.model('Tag', new mongoose.Schema({
    name: { type: String, default: '' },
    articles: [{ type: mongoose.SchemaTypes.ObjectId, ref: 'Article', default: [] }]
}))

const About = mongoose.model('About', new mongoose.Schema({
    avatar: { type: String },
    nickname: { type: String },
    motto: { type: String },
    contact: { type: Array },
    tag: { type: Array }
}))

const FriendshipLinks = mongoose.model('FriendshipLinks', new mongoose.Schema({
    name: { type: String },
    url: { type: String }
}))

const Timeline = mongoose.model('Timeline', new mongoose.Schema({
    content: [
        {
            time: { type: String },
            title: { type: String },
            body1: { type: String },
            body2: { type: String },
            body3: { type: String }
        }
    ]
}))

const Visitors = mongoose.model('Visitors', new mongoose.Schema({
    name: { type: String }
}))

const LeaveMessages = mongoose.model('LeaveMessages', new mongoose.Schema({

    nickname: { type: String, default: '' },
    body: { type: String, default: '' },
    time: { type: String, default: '' },
    reply: { type: String, default: '' },
    loginNum: { type: Number, default: 0 }

}))

const AdminUser = mongoose.model('AdminUser', new mongoose.Schema({
    username: { type: String },
    password: {
        type: String,
        select: false,
        set(val) {
            return require('bcrypt').hashSync(val, 10)
        }
    }

}))

module.exports = { Article, AdminUser, About, FriendshipLinks, Timeline, Visitors, LeaveMessages, Tag } 